<template>
  <div class="layout-row plr15 pb15">
    <div class="layout-col flex1">
      <left-top ref="exposeRef" />
      <left-bottom />
    </div>
    <div class="layout-col flex2 mlr15">
      <center-top />
      <center-bottom />
    </div>
    <div class="layout-col flex1">
      <right />
    </div>
  </div>
</template>

<script lang="ts" setup>
import leftTop from '@/views/async/monitor/leftTop'
import leftBottom from '@/views/async/monitor/leftBottom'
import centerTop from '@/views/async/monitor/centerTop'
import centerBottom from '@/views/async/monitor/centerBottom'
import right from '@/views/async/monitor/right'
import scheduled from "@/utils/scheduled";
const { createScheduled } = scheduled();
const { proxy }: any = getCurrentInstance();
const publicStore = proxy.publicStore();
const configStore = proxy.configStore();
const state = reactive({
  ...publicStore.$state,
  params1: [],
  params2: [],
  params3: [],
  params4: [],
});
let exposeRef = $ref()

onMounted(async () => {
  await setInit()
  if(proxy.isNull(publicStore._public.sensorList)) return
  init();
  createScheduled('monitor', 10*1000, () => { init() })
});

const setInit = async () => {
  let tempList = []
  let tempParams1 = []
  let tempParams2 = []
  let tempParams4 = []
  let query1 = {"o": "o_sensor", "p": [publicStore.distributId], "i": 0}
  let query2 = {"o": "o_sensor_star", "p": [publicStore.distributId], i: 1}
  let params = {queryApi1: query1, queryApi2: query2}
  let res = await publicStore.post(params)
  if(proxy.isNull(res)) return
  let type = await getNew(res.queryApi1)
  configStore.type = type
  if(!proxy.isNull(type)) return proxy.toPath('/new', {type: type})
  publicStore._public.sensorList = res.queryApi1
  res.queryApi1.filter(a=>a.class == '0').forEach(v => {
    let exist = res.queryApi2.find(a=>a.sensor_id == v.id)
    let json_list = exist? {offline: exist.offline} : {offline: ''}
    let json_params1 = {"o":"o_r_sensor", "p":[v.id], "i": 1}
    let json_params2 = {"o":"o_sensor_cfg_property", "p":[v.id], "i": 0}
    let json_params4 = {"o": "o_sensor_alarm_log", "p": [publicStore.distributId, v.type], "i": 3}
    tempParams1.push(json_params1)
    tempParams2.push(json_params2)
    // if(json_params4.p[1].indexOf('PD') != -1 || json_params4.p[1].indexOf('pd') != -1) json_params4.p[1] = 'PD'
    if (!tempParams4.some(a => a.p[1] === v.type)) tempParams4.push(json_params4)
    tempList.push(Object.assign(v, json_list))
  })
  state.params1 = tempParams1
  state.params2 = tempParams2
  state.params4 = tempParams4
  state.list = tempList
};

const getNew = async(sensorList) => {
  let tempArr = []
  sensorList.filter(a=>a.class == '0').forEach(v => {
    let exist = tempArr.find(a=>a == v.type)
    if(!exist && v.type != 'camera') tempArr.push(v.type)
  })
  return tempArr.length == 1? tempArr[0] : ''
}

const init = async () => {
  getList()
  getAlarmList()
  getAlarmType()
  // exposeRef.init()
};

const getList = async() => {
  let params = {queryApi4: state.params1, queryApi5: state.params2}
  let res = await publicStore.post(params)
  state.list.forEach(v => {
    let r_sensor = res.queryApi4.find(a=>a.sensor_id == v.id)
    let cfg_sensor = res.queryApi5.filter(a=>a.sensor_id == v.id)
    v = Object.assign(v, r_sensor, setCfg(cfg_sensor))
  });
  getCount()
}

const setCfg = (cfg_sensor) => {
  let temp = {}
  cfg_sensor.forEach(vv => {
    temp[vv.code] = vv.value
  });
  return temp
}

const getCount = async() => {
  let tempArr = []
  state.list.forEach(v => {
    let temp = tempArr.find(a=>a.type == v.type)
    if(temp){
      temp.count ++
      if(v.isAlarm && v.isAlarm!='0') temp.isAlarm ++ 
      if(v.offline == 1) temp.offline ++ 
      if(v.offline == 0) temp.online ++
      temp.list.push(v)
    }else{
      let name = proxy.find(configStore.templateList, ['type', v.type], 'name')
      let json_temp = {order: 0, list: [v], type: v.type, name: name, count: 1, isAlarm: v.isAlarm && v.isAlarm!='0'? 1 : 0, offline: v.offline == 1? 1 : 0, online: v.offline == 0? 1 : 0}
      if(v.type == 'PD_JC_CSB_CYG') json_temp.order = 1
      tempArr.push(json_temp)
    }
  });
  console.log('tempArr', tempArr)
  tempArr.sort((a, b) => b.order - a.order);
  publicStore._public.countList = tempArr;
  exposeRef.init()
  if(!proxy.isNull(publicStore._public.cameraList )) return
  let cameraList = proxy.find(publicStore._public.countList, ['type', 'camera'], 'list')
  publicStore._public.cameraList = proxy.isNull(cameraList)? [] : cameraList
  console.log('cameraList', publicStore._public.cameraList)
};

const getAlarmList = async() => {
  let query = {"o": "o_sensor_alarm_log", "p": [publicStore.distributId], "i": 0, s: 0, n: 20}
  let params = {queryApi6: query}
  let res = await publicStore.post(params)
  publicStore._public.alarmList = proxy.isNull(res)? [] : res;
}

const getAlarmType = async() => {
  let params = {queryApi7: state.params4}
  let res = await publicStore.post(params)
  res.forEach(v => {
    v.name = v.alarm_name
    v.value = parseInt(v.alarm_count)
  });
  publicStore._public.alarmType = res;
}

</script>

<style scoped lang="scss">

</style>
  